package com.example.myapplication;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.myapplication.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

        private BottomNavigationView bottomNavigationView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                bottomNavigationView = findViewById(R.id.bottom_navigation);
                bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
                        switch (item.getItemId()) {
                                case R.id.nav_product_home:
                                        loadFragment(new HomeFragment());
                                        return true;
                                case R.id.nav_product_list:
                                        loadFragment(new ProductListFragment());
                                        return true;
                                case R.id.nav_transaction:
                                        loadFragment(new TransactionFragment());
                                        return true;
                                case R.id.nav_review:
                                        loadFragment(new ReviewFragment());
                                        return true;
                        }
                        return false;
                });

                // 默认加载商品主页Fragment
                loadFragment(new ProductListFragment());
        }

        private void loadFragment(Fragment fragment) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, fragment);
                transaction.commit();
        }
}
